﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using Com.AlbLab.GEP.Security;

namespace GEP
{
    public class Global : System.Web.HttpApplication
    {

        protected void Application_Start(object sender, EventArgs e)
        {
             
        }

        protected void Session_Start(object sender, EventArgs e)
        {

        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {
            if (HttpContext.Current.User != null)
            {
                if ((HttpContext.Current.User.Identity.IsAuthenticated))
                {
                    MyPrincipal upro = (MyPrincipal)System.Web.HttpContext.Current.Cache.Get(System.Web.HttpContext.Current.User.Identity.Name);
                    if ((System.Web.HttpContext.Current.Cache.Get(System.Web.HttpContext.Current.User.Identity.Name) == null))
                    {
                        // distruggo l'account corrente 
                        System.Web.HttpContext.Current.User = null;

                        // distruggo il ticket di autenticazione 
                        System.Web.Security.FormsAuthentication.SignOut();

                        // abbandono la sessione 
                        if (((System.Web.HttpContext.Current.Session != null)))
                        {
                            System.Web.HttpContext.Current.Session.Abandon();
                        }
                        Response.Redirect(((Global)sender).Context.Request.ApplicationPath + "/login.aspx?ForceClose=2");
                    }
                    else
                    {
                        HttpContext.Current.User = (MyPrincipal)System.Web.HttpContext.Current.Cache.Get(System.Web.HttpContext.Current.User.Identity.Name);

                    }
                }
            }
        }
        

        protected void Application_Error(object sender, EventArgs e)
        {

        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {

        }
    }
}